﻿//***************************************************************
// 脚本名称：JointPoints
// 类创建人：周  波
// 创建日期：2015.12
// 功能描述：常用子接点（名称，接点）
//***************************************************************

using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// 挂接点类型
/// </summary>
internal class JointNamesConst
{
    /// <summary>
    /// 名称
    /// </summary>
    public const string Name = "Name";
    /// <summary>
    /// Buff 挂接点
    /// </summary>
    public const string Buff = "Buff";
    /// <summary>
    /// 伤害挂接点
    /// </summary>
    public const string Hurm = "Hurm";
    /// <summary>
    /// 子弹挂接点
    /// </summary>
    public const string Bullet = "Bullet";
    /// <summary>
    /// 受击挂接点
    /// </summary>
    public const string Hit = "Hit";
    /// <summary>
    /// 两脚中心挂接点
    /// </summary>
    public const string Base = "Base";
    /// <summary>
    /// 头部挂接点
    /// </summary>
    public const string Head = "Head";
    /// <summary>
    /// 胸口挂接点
    /// </summary>
    public const string Chest = "Chest";
    /// <summary>
    /// 武器左挂接点
    /// </summary>
    public const string WeaponLeft = "WeaponLeft";
    /// <summary>
    /// 武器右挂接点
    /// </summary>
    public const string WeaponRight = "WeaponRight";
}

/// <summary>
/// 玩具效果的挂接点
/// </summary>
public class JointPoints : MonoBehaviour
{
    /// <summary>
    /// 原点挂接点（两脚中心）
    /// </summary>
    [SerializeField]
    Transform BaseJoint = null;

    /// <summary>
    /// 其他的挂机点
    /// </summary>
    [SerializeField]
    [Tooltip("特效挂机点名称和Transform的名称相同")]
    Transform[] OtherJoints = null;

    private Dictionary<string, Transform> jointCache = new Dictionary<string, Transform>();

    /// <summary>
    /// 获取到挂接点
    /// </summary>
    /// <param name="jointName">挂接点名字</param>
    /// <returns>挂接点(如果字典中没有此挂节点，则默认为自身跟节点)</returns>
    public Transform GetJointTransform(string jointName)
    {
        if (jointCache.ContainsKey(jointName))
        {
            return jointCache[jointName];
        }
        else
        {
            return transform;
        }
    }

    void Start()
    {
        if (BaseJoint != null)
        {
            jointCache.Add(JointNamesConst.Base, BaseJoint);
        }

        if (OtherJoints != null)
        {
            for (int i = 0; i < OtherJoints.Length; i++)
            {
                Transform joint = OtherJoints[i];
                if (joint != null)
                {
                    if (!jointCache.ContainsKey(joint.name))
                    {
                        jointCache.Add(joint.name, joint);
                    }
                    else
                    {
                        Debug.LogErrorFormat("Joint name[{0}] repeated in transform [{1}], please check it", joint.name, transform.name);
                    }
                }
            }
        }
    }
}
